Add the convenience method Option::is_some_and
#75298
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds:
I recently got the idea for this method. It solves a "pain point" (albeit a small one) I encountered many times over the years: checking if an
Option
isSome(x)
and if a condition holds forx
. Currently possible solutions:Verbose:
Concise, but the
unwrap
is not nice :/Somewhat concise, but not easy to read. At least I have to actively think about it for a few seconds before I know what exactly it does.
Concise and probably the best solution currently. However, uses a macro and the "reading flow" is not optimal (due to the two
if
s).With the new method, it would look like this:
I think this is a bit better than the
matches!
version. However, I understand there are a bunch of reasons not to add this method:Option
already has lots of helper methods and expanding the API surface has some disadvantages. If the advantage over thematches!
solution is too small, addingis_some_and
might not be worth it.true
in theNone
case. If that use case is equally as common (it is not in my experience), there should be a method for that, too. Something likeis_none_or
. But adding two methods is even worse than one, so it might not be worth it.and
in the name might be confusing because of its use inOption::and
andOption::and_then
.So I can totally understand if we decide against this addition. Just wanted to hear people's opinion on this :)
I tried searching for previous attempts to add such a method, but didn't find anything. If I missed something, please let me know.